/** ********************************************************************
 * File:           InputDialog.java 
 * Description:    Creates a resizeable User Input Dialog Box using SWT
 * Authors:        Akshay,Abhinava,Revati,Arun 
 * Created:        Sun Oct 26 10:31:25 IST 2008
 *
 * (C) Copyright 2008, MILE Lab, IISc
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 ** http://www.apache.org/licenses/LICENSE-2.0
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 *
 **********************************************************************/
package core;

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class InputDialog {

    String value = null;
    String language;

    // This is just a reference to the main display object. Main display object
    // has been
    // passed as parameter from the CKI class.
    Display copyDisplay;

    InputDialog(Display mainDisplay, String lang) {
        copyDisplay = mainDisplay; // Initializing the reference display object.
        // Refers to the main display
        language = lang;
    }

    public String open() {

        final Clipboard cb = new Clipboard(copyDisplay);
        final Image image = new Image(copyDisplay, CKI.class.getResourceAsStream("pifmgr1.ico"));

        /* The input window shell */
        final Shell inputShell = new Shell(copyDisplay);

        /* The shell which takes care of the keyboard layout image */
        final Shell layoutImage = new Shell(copyDisplay);
        inputShell.setText("INPUT - " + language);
        inputShell.setLayout(new FormLayout());

        /* The output window shell */
        final Shell outputShell = new Shell(copyDisplay);
        outputShell.setText("OUTPUT - " + language);
        outputShell.setLayout(new FormLayout());

        /* This is the text box of the input window. */
        final Text text = new Text(inputShell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP | SWT.LEFT); /*
         * SWT.SINGLE can be replaced with
         * SWT.MULTI for multi line input. When
         * SWT.WRAP is added, SWT.H_SCROLL has
         * no effect
         */

        /* This is the text box of the output window.It's READ-ONLY */
        final Text textOutput = new Text(outputShell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP | SWT.LEFT | SWT.READ_ONLY);

        /* Label of the input window */
        Label label = new Label(inputShell, SWT.SHADOW_OUT | SWT.BORDER | SWT.CENTER);
        label.setText("Enter your text here");

        /* Label of the output window */
        Label labelOutput = new Label(outputShell, SWT.SHADOW_OUT | SWT.BORDER | SWT.CENTER);
        labelOutput.setText("Text after conversion to Unicode ");

        /* The buttons of the input window go here */
        final Button OK = new Button(inputShell, SWT.PUSH | SWT.CENTER);
        OK.setText("OK");
        OK.setToolTipText("OK");
        OK.addListener(SWT.Selection, new Listener() {

            public void handleEvent(Event e) {
                value = text.getText();
                inputShell.dispose();
            }
        });

        Button cancel = new Button(inputShell, SWT.PUSH | SWT.CENTER);
        cancel.setText("Cancel");
        cancel.setToolTipText("Closes the window");
        cancel.addListener(SWT.Selection, new Listener() {

            public void handleEvent(Event e) {

                Shell sh = new Shell(copyDisplay, SWT.APPLICATION_MODAL);
                sh.setImage(image);
                MessageBox messageBox = new MessageBox(sh, SWT.YES | SWT.NO | SWT.ICON_QUESTION);
                messageBox.setText("QUIT");

                messageBox.setMessage("Are you sure to quit?? The contents will be lost!!");
                int rCode = messageBox.open();
                if (rCode == SWT.YES) {
                    value = null;
                    inputShell.dispose();

                } else {
                }
            }
        });

        final Button copyToClip = new Button(inputShell, SWT.PUSH | SWT.CENTER);
        copyToClip.setText("Copy to\nClipboard");
        copyToClip.setToolTipText("Copies the entire text\nto clipboard");
        copyToClip.addListener(SWT.Selection, new Listener() {

            public void handleEvent(Event event) {

                text.selectAll();
                // text.copy();
                String textData = text.getSelectionText();
                if (textData.length() > 0) {
                    TextTransfer textTransfer = TextTransfer.getInstance();
                    cb.setContents(new Object[]{textData},
                            new Transfer[]{textTransfer});
                }
            }
        });

        final Button Clear = new Button(inputShell, SWT.PUSH | SWT.CENTER);
        Clear.setText("Clear\nAll");
        Clear.setToolTipText("Clears the entire text");
        Clear.addListener(SWT.Selection, new Listener() {

            public void handleEvent(Event event) {

                text.setText("");
                textOutput.setText("");

            }
        });
        /* The buttons of the input window end here */

        text.setToolTipText("Text area.To increase\nhorizontal line width,\nresize the window");
        textOutput.setToolTipText("Output text area.\nThis is a *read-only* field.\nYou can only copy the contents");

        /* The input text-box event listener */
        text.addListener(SWT.Modify, new Listener() {

            public void handleEvent(Event event) {

                if (text.getText().length() > 0) {
                    OK.setEnabled(true);
                    copyToClip.setEnabled(true);
                    Clear.setEnabled(true);
                } else {
                    OK.setEnabled(false);
                    copyToClip.setEnabled(false);
                    Clear.setEnabled(false);
                }
                /*
                 * This line below outputs the text entered in the input
                 * text-box to the output text box
                 */

                if (text.getText().length() == 0) {
                    textOutput.setText("");
                }

                if (language.equals("KaGaPa") || language.equals("Microsoft Inscript")) {
                    Kannada k1 = new Kannada();
                    StringBuilder a = new StringBuilder();
                    char b = 0;
                    for (int i = 0; i < text.getText().length(); i++) {

                        b = k1.convert(text.getText().charAt(i));
                        a.append(b);
                        textOutput.setText(a.toString());
                    }

                // textOutput.setText(text.getText());
                } else if (language.equals("Tamilnet99")) {
                    Tamil t1 = new Tamil();
                    StringBuilder a = new StringBuilder();
                    char b = 0;
                    for (int i = 0; i < text.getText().length(); i++) {

                        b = t1.convert(text.getText().charAt(i));
                        a.append(b);
                        textOutput.setText(a.toString());
                    }

                // textOutput.setText(text.getText());
                }
            }
        });

        inputShell.addListener(SWT.Traverse, new Listener() {

            public void handleEvent(Event event) {
                if (event.detail == SWT.TRAVERSE_ESCAPE) {
                    event.doit = false;
                }
            }
        });

        /* This is the copy button of the output window */
        final Button copyButtonOfOutput = new Button(outputShell, SWT.PUSH | SWT.CENTER);
        copyButtonOfOutput.setText("Copy to\nClipboard");
        copyButtonOfOutput.setToolTipText("Copies the entire text\nto clipboard");
        copyButtonOfOutput.addListener(SWT.Selection, new Listener() {

            public void handleEvent(Event event) {

                textOutput.selectAll();
                String textData = textOutput.getSelectionText();
                if (textData.length() > 0) {
                    TextTransfer textTransfer = TextTransfer.getInstance();
                    cb.setContents(new Object[]{textData},
                            new Transfer[]{textTransfer});
                }
            }
        });

        FormData data;

        data = new FormData();
        data.left = new FormAttachment(0, 25);
        data.top = new FormAttachment(0, 35);
        data.right = new FormAttachment(100, -10);
        data.bottom = new FormAttachment(100, -55);
        text.setLayoutData(data);

        data = new FormData();
        data.width = 72;
        data.height = 43;
        data.left = new FormAttachment(120, 700, 0);
        // data.left = new FormAttachment(100, 250, 0);
        // data.right = new FormAttachment(cancel,-20,SWT.LEFT);
        data.top = new FormAttachment(100, -48);
        OK.setLayoutData(data);

        data = new FormData();
        data.width = 72;
        data.height = 43;
        data.left = new FormAttachment(OK, 20, SWT.RIGHT);
        // data.right = new FormAttachment(copyToClip,-20,SWT.LEFT);
        data.top = new FormAttachment(100, -48);
        cancel.setLayoutData(data);

        data = new FormData();
        data.width = 72;
        data.height = 43;
        data.left = new FormAttachment(cancel, 20, SWT.RIGHT);
        // data.right = new FormAttachment(Clear,-20,SWT.LEFT);
        data.top = new FormAttachment(100, -48);
        copyToClip.setLayoutData(data);

        data = new FormData();
        data.width = 72;
        data.height = 43;
        data.left = new FormAttachment(copyToClip, 20, SWT.RIGHT);
        // data.right = new FormAttachment(120,700,0);
        data.top = new FormAttachment(100, -48);
        Clear.setLayoutData(data);

        data = new FormData();
        data.left = new FormAttachment(0, 25);
        data.top = new FormAttachment(0, 10);
        data.right = new FormAttachment(100, -27);
        // data.bottom = new FormAttachment(100, -55);
        label.setLayoutData(data);

        OK.setEnabled(false);
        copyToClip.setEnabled(false);
        Clear.setEnabled(false);

        data = new FormData();
        data.left = new FormAttachment(0, 25);
        data.top = new FormAttachment(0, 35);
        data.right = new FormAttachment(100, -10);
        data.bottom = new FormAttachment(100, -55);
        textOutput.setLayoutData(data);

        data = new FormData();
        data.left = new FormAttachment(0, 25);
        data.top = new FormAttachment(0, 10);
        data.right = new FormAttachment(100, -27);
        // data.bottom = new FormAttachment(100, -55);
        labelOutput.setLayoutData(data);

        data = new FormData();
        data.width = 72;
        data.height = 43;
        data.left = new FormAttachment(300, 700, 0);
        // data.left = new FormAttachment(100, 250, 0);
        // data.right = new FormAttachment(cancel,-20,SWT.LEFT);
        data.top = new FormAttachment(100, -48);
        copyButtonOfOutput.setLayoutData(data);

        layoutImage.setSize(1006, 290);
        layoutImage.setLocation(copyDisplay.getClientArea().width - 500 - 506,
                copyDisplay.getClientArea().height - 500 - 313);
        final Image kgp = new Image(copyDisplay, CKI.class.getResourceAsStream("kgp.png"));
        final Image inscript = new Image(copyDisplay, CKI.class.getResourceAsStream("inscript.png"));
        if (language.compareTo("KaGaPa") == 0) {
            layoutImage.setText("KaGaPa Kannada Layout");
            layoutImage.setBackgroundImage(kgp);
            layoutImage.open();
            layoutImage.forceFocus();
        } else if (language.compareTo("Microsoft Inscript") == 0) {
            // layout.setSize(1006, 290);
            // layout.setLocation(copyDisplay.getClientArea().width - 500 - 506,
            // copyDisplay.getClientArea().height - 500 - 314);
            layoutImage.setText("Inscript Kannada Layout");
            layoutImage.setBackgroundImage(inscript);
            layoutImage.open();
            layoutImage.forceFocus();
        }
        layoutImage.setImage(image);

        outputShell.setImage(image);
        outputShell.setSize(500, 500);
        outputShell.setLocation(copyDisplay.getClientArea().width - 1000,
                copyDisplay.getClientArea().height - 500);
        outputShell.open();
        outputShell.forceFocus();

        inputShell.setImage(image);
        inputShell.setSize(500, 500);
        inputShell.setLocation(copyDisplay.getClientArea().width - 500, copyDisplay.getClientArea().height - 500);
        inputShell.open();
        inputShell.forceFocus();

        while (!inputShell.isDisposed()) {
            if (!copyDisplay.readAndDispatch()) {
                copyDisplay.sleep();
            }
        }
        image.dispose();
        kgp.dispose();
        inscript.dispose();
        cb.dispose();
        inputShell.dispose();
        outputShell.dispose();
        layoutImage.dispose();
        return value;
    }

    /*
     * public static void main(String[] args) { Shell shell = new Shell();
     * InputDialog dialog = new InputDialog(shell);
     * System.out.println(dialog.open()); }
     */
}
